package com.mj.mafeed.service;

import org.codehaus.jackson.map.ObjectMapper;

import android.content.Context;

import com.mj.mafeed.bean.MemberJSON;
import com.mj.mafeed.config.Constants;
import com.mj.mafeed.util.HttpUtils;
import com.mj.mafeed.util.Md5Util;
/**
 * 会员
 * @author zhaominglei
 * @date 2014-12-15
 * 
 */
public class MemberService extends BaseService {
	private CartItemService cartItemService = new CartItemService();
	
	/**
	 * 发送验证码  单向的 暂时都返回true
	 */
	public boolean sendCode(Context context, String referer, String mobile) {
		StringBuilder param = new StringBuilder();
		param.append("moblies=").append(mobile).append("&type=0");
		HttpUtils.doPost(Constants.SENDMSG_URL, param.toString());
		return true;
	}
	
	/**
	 * 注册
	 */
	public MemberJSON register(String postData) {
		String result = HttpUtils.doPost(Constants.MEMBER_REGISTER_URL, postData);
		if (result != null && !result.equals("")) {
			try {
				MemberJSON memberJSON = new ObjectMapper().readValue(result, MemberJSON.class);
				return (memberJSON != null) ? memberJSON : null;
			}  catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 找回密码
	 */
	public MemberJSON findpwd(String postData) {
		String result = HttpUtils.doPost(Constants.MEMBER_FINDPWD_URL, postData);
		if (result != null && !result.equals("")) {
			try {
				MemberJSON memberJSON = new ObjectMapper().readValue(result, MemberJSON.class);
				return (memberJSON != null) ? memberJSON : null;
			}  catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}	
	
	/**
	 * 登录
	 */
	public MemberJSON login(Context context, String referer, String username, String password) {
		StringBuilder param = new StringBuilder();
		param.append("username=").append(username).append("&password=").append(Md5Util.encrypt(password));
		String result = HttpUtils.doPost(Constants.MEMBER_LOGIN_URL, param.toString());
		if (result != null && !result.equals("")) {
			try {
				MemberJSON memberJSON = new ObjectMapper().readValue(result, MemberJSON.class);
				if (memberJSON != null) { 
					if (referer != null && referer.equals("CartActivity")) { //来自购物车
						cartItemService.syncCartItem(context, referer, username, password); 
					}
					return memberJSON; 
				} 
			}  catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 修改个人资料
	 */
	public boolean updateProfile(String referer, String email, String name, String username, String sign) {
		StringBuilder param = new StringBuilder();
		param.append("email=").append(email).append("&name=").append(name);
		param.append("&username=").append(username).append("&sign=").append(sign);
		String result = HttpUtils.doPost(Constants.MEMBER_PROFILE_UPDATE_URL, param.toString());
		if (result != null && !result.equals("")) {
			if (result.equals("true")) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 修改密码
	 */
	public boolean updatePassword(String referer, String pPassword, String cfnPassword, String username, String sign) {
		StringBuilder param = new StringBuilder();
		param.append("password=").append(Md5Util.encrypt(pPassword)).append("&nPassword=").append(Md5Util.encrypt(cfnPassword));
		param.append("&username=").append(username).append("&sign=").append(sign);
		String result = HttpUtils.doPost(Constants.MEMBER_PASSWORD_UPDATE_URL, param.toString());
		if (result != null && !result.equals("")) {
			if (result.equals("true")) {
				return true;
			}
		}
		return false;
	}
}
